package com.systemdevelopment.mainproject.View;

import com.jfoenix.controls.JFXDrawer;

import com.systemdevelopment.mainproject.Controller.*;
import com.systemdevelopment.mainproject.Model.*;
import com.systemdevelopment.mainproject.View.Admin.configBookController;
import com.systemdevelopment.mainproject.View.Visitor.SearchPaneController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.util.Objects;
import java.util.Stack;

public class mainUI extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    public Parent root;//根节点
    public Scene scene;//根节点对应的场景
    public static VBox ContentPane;//浏览页面容器
    public static UserController userController;
    private static Stack<Pane> contentPanes;//页面堆栈

    public static JFXDrawer sideMenu;//选项面板容器
    public static Book CurBook;
    @Override
    public void start(Stage primaryStage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(mainUI.class.getResource("mainPane.fxml"));
        scene = new Scene(fxmlLoader.load());
        root =scene.getRoot();
        contentPanes=new Stack<>();

        CurBook=null;
        ContentPane= (VBox) root.lookup("#contentPane");
        sideMenu= (JFXDrawer) root.lookup("#sideMenu");
        primaryStage.setTitle("MainWindow");
        primaryStage.setScene(scene);
        primaryStage.setResizable(true);
        primaryStage.setHeight(800);
        primaryStage.setWidth(1000);
        setUserController(new VisitorController());
        root.getStylesheets().add(Objects.requireNonNull(getClass().getResource("blue_theme.css")).toExternalForm());




        primaryStage.show();
    }
    public static Object getFXMLData(String path) throws IOException {
        return (new FXMLLoader(mainUI.class.getResource(path))).load();
    }
    public static void loadLoginPane()
    {
        loadContentPane("./Visitor/loginPane.fxml");
    }
    public static void loadRegisterPane()
    {
        loadContentPane("./Visitor/registerPane.fxml");
    }
    public static void loadSearchPane(){
        loadContentPane("./Visitor/searchPane.fxml");
        ((SearchPaneController)oneInstanceController.getController("SearchPaneController")).loadAllBooks();

    }
    public static void loadBookContentPane(){loadContentPane("./Visitor/bookContentPage.fxml");}
    public static void loadUserPane()
    {
        loadContentPane("./NormalUser/userRecord.fxml");
    }
    public static void loadConfigBookPane(Book book)
    {
        loadContentPane("./Admin/configBookPane.fxml");
        ((configBookController)oneInstanceController.getController("configBookController")).initAuthorBox();
        ((configBookController)oneInstanceController.getController("configBookController")).loadBook(book);
    }
    public static void loadWelcomePage()
    {
        loadContentPane("./welcomePane.fxml");

        ((welcomePaneController)oneInstanceController.getController("welcomePaneController")).welcomeLabel.setText(
                "您好，"+userController.getUser().Account);

    }
    public static void loadBackPane()//回到上个页面
    {
        if(contentPanes.size()>1)
        {
            ContentPane.getChildren().removeAll();
            contentPanes.pop();
            ContentPane.getChildren().setAll(contentPanes.peek());
        }
    }
    public static void popContentPanes()
    {
        contentPanes.clear();
        ContentPane.getChildren().removeAll();
        loadWelcomePage();
    }
    public static void loadContentPane(String url)//加载页面
    {
        try {
            Pane p =  PaneFactory.getPane(url);
            ContentPane.getChildren().removeAll();
            ContentPane.getChildren().setAll(p);
            contentPanes.push(p);
        } catch (IOException e) {
            System.out.println(url+"加载错误");
            e.printStackTrace();
        }
    }
    public static NormalUserController getNormalUserController()
    {
        assert userController.getClass().getSimpleName().equals("NormalUserController");
        return (NormalUserController) (userController);
    }
    public static VisitorController getVisitorController()
    {
        assert userController.getClass().getSimpleName().equals("VisitorController");
        return (VisitorController) (userController);
    }
    public static SuperAdminController getSuperAdminController()
    {
        assert userController.getClass().getSimpleName().equals("SuperAdminController");
        return (SuperAdminController) (userController);
    }
    public static AdminController getAdminController()
    {
        assert userController.getClass().getSimpleName().equals("AdminController");
        return (AdminController) (userController);
    }
    public static void setUserController(UserController controller)
    {
        if(controller!=null)
        {
            String url;
            userController=controller;
            switch(controller.getClass().getSimpleName())
            {
                case "SuperAdminController":
                    url="SuperAdmin/SuperAdminMenu.fxml";
                    break;

                case "AdminController":
                    url="Admin/AdminMenu.fxml";
                    break;

                case "NormalUserController":
                    url="NormalUser/MenuPane.fxml";
                    break;

                default:
                    url="Visitor/MenuPane.fxml";
                    break;
            }
            try {
                sideMenu.setSidePane(PaneFactory.getPane(url));
                popContentPanes();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static String clobToString(Clob clob) throws Exception {
        String re = "";
        Reader is = null;
        BufferedReader br = null;
        try {
            // 得到流
            is = clob.getCharacterStream();
            br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            // 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
            while (s != null) {
                sb.append(s);
                s = br.readLine();
            }
            re = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (is != null) {
                is.close();
            }
            if (br != null) {
                br.close();
            }
        }
        return re;
    }

}
